package com.cop.consultapp.adapter;

import java.util.ArrayList;

import com.cop.consultapp.activity.ConsultInsertActivity;
import com.cop.consultapp.activity.ConsultViewActivity;
import com.cop.consultapp.model.QuestionVO;
import com.kdn.consaltapp.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ConsultListAdapter extends BaseAdapter {

	private Context context = null;
	private int layout;
	private ArrayList<QuestionVO> consultList = null;
	
	private LayoutInflater inflater = null;
	
	
	public ConsultListAdapter(Context context, int layout, ArrayList<QuestionVO> src) {
		this.context = context;
		this.layout = layout;
		this.consultList = src;
		
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		
	}

	@Override
	public int getCount() {
		if(this.consultList==null)		return 0;
		
		return this.consultList.size();
	}

	@Override
	public Object getItem(int position) {
		if(this.consultList==null)		return null;
		return this.consultList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		//1. 하나의 아이템을 그릴 바탕 xml을 불러온다.
		if(convertView==null){
			//최초로 그리는 경우
			convertView = this.inflater.inflate(this.layout, parent, false);
		}		
		
		TextView txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
		TextView txtName = (TextView) convertView.findViewById(R.id.txtName);
		
		//2. 값을 제자리에 세팅
		QuestionVO myItem = (QuestionVO) getItem(position);
		
		txtTitle.setText(myItem.getTitle());
		txtName.setText(myItem.getName());
		
		
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				QuestionVO myItem = (QuestionVO) getItem(position);
				Intent intent=new Intent(context, ConsultViewActivity.class);
				
				//String inputMsg=editMsg.getText().toString();				
				Bundle inputBundle=new Bundle();
				inputBundle.putString("IDX", myItem.getIdx()+"");				
				intent.putExtras(inputBundle);
				//Toast.makeText(context, myItem.getIdx()+"", Toast.LENGTH_LONG).show();
				((Activity)context).startActivity(intent);
				
				
				
			}
		});
		
		/*btn.setOnClickListener(new OnClickListener() {
			
			
			@Override
			public void onClick(View arg0) {
				
				MyItem myItem = (MyItem) getItem(position);
				
				Toast.makeText(context, myItem.getName(), Toast.LENGTH_LONG).show();
				
			}
		}); */
		
		//3. 생성된 아이템을 return		
		return convertView;
	}

}
